package com.chaos.auth.config.grant.converter.helper;


import com.chaos.framework.model.enums.ResultEnum;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
import org.springframework.security.oauth2.core.OAuth2Error;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.Map;

public class OAuth2EndpointHelper {

    public static MultiValueMap<String, String> getParameters(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        MultiValueMap<String, String> parameters = new LinkedMultiValueMap(parameterMap.size());
        parameterMap.forEach((key, values) -> {
            if (values.length > 0) {
                String[] var3 = values;
                int var4 = values.length;

                for (int var5 = 0; var5 < var4; ++var5) {
                    String value = var3[var5];
                    parameters.add(key, value);
                }
            }

        });
        return parameters;
    }

    public static void throwError(Integer errorCode, String description) {
        OAuth2Error error = new OAuth2Error(errorCode.toString(), description, "");
        throw new OAuth2AuthenticationException(error);
    }

    public static void throwError(ResultEnum resultEnum) {
        OAuth2Error error = new OAuth2Error(resultEnum.getCode().toString(), resultEnum.getMsg(), "");
        throw new OAuth2AuthenticationException(error);
    }

}